
clear all
close all
clc


n = 1000; 
sigma=0.3;

[Xapp,yapp,xtest,ytest]=datasets('Mixture',n,100,sigma);
Xapp=single(Xapp);
[Xapp xtest]=normalizemeanstd(Xapp,xtest);

idxtrp = find(yapp == 1);
idxtrn = find(yapp == -1);

trfigure = figure; 
plot(Xapp(idxtrp,1),Xapp(idxtrp,2),'rx'),hold on,plot(Xapp(idxtrn,1),Xapp(idxtrn,2),'bo'), hold off;

idxtp = find(ytest == 1);
idxtn = find(ytest == -1);

tstfigure = figure; 
plot(xtest(idxtp,1),xtest(idxtp,2),'rx'),hold on,plot(xtest(idxtn,1),xtest(idxtn,2),'bo'), hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Train an adaboost classifier to learn the two classes.

niter = 100;

% Train the adaboost classifier
adamodel = trainadaboost(Xapp,yapp,1,niter);

% Test adaboost on the test data.
[error tstadaaccuracy yest] = eval_multiclass_boost(xtest,ytest,adamodel,niter);
% disp(sprintf('Adaboost alone Test accuracy: %f',tstadaaccuracy*100));

%%% Plot each of the decision stumps, one by one.
xmin = -3;
xmax = 3;
ymin = -3;
ymax = 3;

figure(trfigure);

axis([xmin xmax ymin ymax]);

for i = 1 : min(niter,size(adamodel{1},1))
    figure(trfigure),
    
    plot(Xapp(idxtrp,1),Xapp(idxtrp,2),'rx'),hold on,plot(Xapp(idxtrn,1),Xapp(idxtrn,2),'bo'),
    axis([xmin xmax ymin ymax]);
    
    if adamodel{1}{i}.ind == 1
        if adamodel{1}{i}.s == 1
            p = patch([adamodel{1}{i}.x0 xmax xmax adamodel{1}{i}.x0],[ymin ymin ymax ymax],'y');alpha(0.5), hold on;
            set(p,'edgecolor','none'), hold on,
        else
            p = patch([xmin adamodel{1}{i}.x0 adamodel{1}{i}.x0 xmin],[ymin ymin ymax ymax],'y');alpha(0.5), hold on;
            set(p,'edgecolor','none'), hold on,
        end
    else
        if adamodel{1}{i}.s == 1
            p = patch([xmin xmin xmax xmax],[adamodel{1}{i}.x0 ymax ymax adamodel{1}{i}.x0],'y');alpha(0.5), hold on;
            set(p,'edgecolor','none'), hold on,
        else
            p = patch([xmin xmin xmax xmax],[ymin adamodel{1}{i}.x0 adamodel{1}{i}.x0 ymin],'y');alpha(0.5), hold on;
            set(p,'edgecolor','none'), hold on,
        end
    end
    hold off;
    pause;
end
